#include "pick_up_widget.h"

#include <QGuiApplication>
#include <QScreen>
#include <QDebug>

PickupMainWindow::PickupMainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    QRect screen = QGuiApplication::primaryScreen()->geometry();
    resize(screen.size());
    setWindowTitle(QStringLiteral("pickup"));
}

PickupMainWindow::~PickupMainWindow() {}

bool PickupMainWindow::Start()
{
    m_appWidget = new QWidget();
    if (m_appWidget == Q_NULLPTR)
        return false;

    m_appWidget->setObjectName("pickup_app_widget");
    m_appWidget->setStyleSheet("QWidget#pickup_app_widget{background:transparent;}");
    m_appWidget->setAttribute(Qt::WA_TranslucentBackground);

    // 设置窗口透明度
    m_appWidget->setWindowOpacity(0.5);

    // 设置窗口透明度
    m_appWidget->setWindowOpacity(0.5);

    this->setCentralWidget(m_appWidget);

    bool ret = SetLayout();
    if(!ret)
        return ret;
    
    ret = SetAddFileButton();
    if(!ret)
        return ret;
    
    ret = SetActions();
    if(!ret)
        return ret;

    return true;
}

void PickupMainWindow::OnAddFileButtonClicked()
{
    static int buttonFileRow = 0;
    QString filename = QFileDialog::getOpenFileName(this, "打开文件", QString(), "All Files (*);;Text Files (*.txt)");
    if (filename.isEmpty())
        return;
    buttonFileRow++;
    m_pushButtonFile = new FileAnalysisButton(m_appLayout, buttonFileRow, 0);
    qDebug() << "open file: " << filename;
}

bool PickupMainWindow::SetActions()
{
    connect(m_buttonAddFile, &QPushButton::clicked, this, &PickupMainWindow::OnAddFileButtonClicked);
    return true;
}

bool PickupMainWindow::SetAddFileButton()
{
    m_buttonAddFile = new QPushButton();
    if (m_buttonAddFile == Q_NULLPTR)
        return false;

    m_buttonAddFile->setText("添加文件");
    m_appLayout->addWidget(m_buttonAddFile, 0, 0, 1, 1);
    return true;
}

bool PickupMainWindow::SetLayout()
{
    m_appLayout = new QGridLayout();
    if (m_appLayout == Q_NULLPTR)
        return false;

    m_appLayout->setSpacing(0);
    m_appLayout->setContentsMargins(0, 0, 0, 0);
    m_appLayout->setObjectName("pickup_app_layout");
    m_appWidget->setLayout(m_appLayout);

    m_appLayout->setRowStretch(0, 0);
    m_appLayout->setColumnStretch(0, 1);
    return true;
}

bool PickupMainWindow::Destroy()
{
    if (m_appWidget != Q_NULLPTR)
    {
        delete m_appWidget;
        m_appWidget = Q_NULLPTR;
    }
    return true;
}